#include "Http.hpp"
#define WWWROOT "./wwwroot"

void Hello(const HttpRequest &req, HttpResponse *res)
{
    DEBUG_LOG("开始执行业务处理（15S）");
    // sleep(15);
    res->SetBody(req.Serialize(), "text/plain");
}
void Login(const HttpRequest &req, HttpResponse *res)
{
    res->SetBody(req.Serialize(), "text/plain");
}

void Putfile(const HttpRequest &req, HttpResponse *res)
{
    DEBUG_LOG("开始接收文件");
    std::string file_path = WWWROOT+req._path;
    Tool::WriteFile(file_path.c_str(),req._body);
}
void Deletefile(const HttpRequest &req, HttpResponse *res)
{
    res->SetBody(req.Serialize(), "text/plain");
}
int main()
{
    HttpServer http_server(8080, 3);
    http_server.SetRootDir(WWWROOT);
    std::regex hello_regex("/hello");
    http_server.SetGetHandler(hello_regex, Hello);
    std::regex login_regex("/login");
    http_server.SetPostHandler(login_regex, Login);
    std::regex putfile_regex("/6666.txt");
    http_server.SetPutHandler(putfile_regex, Putfile);
    std::regex deletefile_regex("/1234");
    http_server.SetDeleteHandler(deletefile_regex, Deletefile);
    http_server.Start();
    return 0;
}
